package org.freespace.utils;

import org.freespace.model.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {
    public static String USER_REDIS_KEY = "USER";
    public static String ADMIN_REDIS_KEY = "IS_ADMIN_USER";

    public static HttpSession getSession() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            return request.getSession();
        }
        return null;
    }

    public static String getCurrentUserName() {
        User currUser = getCurrentUserInfo();
        if (currUser == null) {
            // throw no_login

        }
        return currUser.getName();
    }

    private static User getCurrentUserInfo() {
        HttpSession session = getSession();
        if (session == null) {
            return null;
        }
        return (User) session.getAttribute(USER_REDIS_KEY);
    }

    public static String getCurrentUser() {
        return "liuqi";
    }
}
